﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccess;
using System.Data;

namespace ControlCasaBO
{
    public abstract class BOBase
    {
        private const string CADENASQL = "SELECT DISTINCT {0}.* FROM {0} INNER JOIN {1} ON {2}={3}";

        protected static List<T> Get<T>(string fromTable, string innerTable, string fromField, string innerField)
        {
            DataAccessDAO dao = DataAccessDAO.GetInstance();

            DataAccessCommand command = new DataAccessCommand(FormatQuery(fromTable, innerTable, fromField, innerField), CommandType.Text);

            List<T> lista = dao.GetList<T>(command);

            var nuevaLista = (from q in lista.Cast<IResult>()
                        orderby q.Descripcion
                        select q);

            lista = nuevaLista.Cast<T>().ToList<T>();

            return lista;
        }
        private static string FormatQuery(string fromTable, string innerTable, string fromField, string innerField)
        {
            return String.Format(CADENASQL, fromTable, innerTable, fromField, innerField);
        }
    }
}
